package com.shop.utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class StringUtils {
	
	public static final Map<String ,String> sqlMap = new HashMap<String ,String>();
	
	
	static{
		/**
		 *  解析 SQLs.xml文件
		 */
		SAXReader saxReader = new SAXReader();
		Document doc= null;
		try {
			doc = saxReader.read(StringUtils.class.getResource("/")+"sqls.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}	
		 Element root=doc.getRootElement();
	     for ( Iterator<?> i = root.elementIterator(); i.hasNext(); ) {
	            Element element = (Element) i.next();
	            sqlMap.put(element.attributeValue("name"),element.getText());
	        }
	}
	
	/**
	 * 根据sql key 获得sql语句并将传来的参数放入其中
	 * **/
	public static String getSql(String key,String...values){
	    
		String sql=sqlMap.get(key);
		for (int i = 0; i < values.length; i++) {
			if(sql.contains("~")){
				sql=sql.replaceFirst("~",values[i]);
			}
		}
	     
		return sql;
	}
	
	/**
	 *  获得 WEB-INF下的资源文件
	 */
	public static String sqlURL(){
		String urls = StringUtils.class.getResource("/")+"";
		
		return urls.substring(0, urls.lastIndexOf("classes"));		
	}
	
	/**
	 * URL titleID = ***8+* 
	 * @param titleId
	 * @return
	 */
	public static int titleIdToInt(String titleId){
		if(titleId ==null || "".equals(titleId)){
			return 0;
		}
		return  Integer.parseInt(titleId.substring(3,titleId.length()-2));
	}
	
	/**
	 * 获得时间戳  = 毫秒数+三位随机数
	 */
	public static String getTimeCode(){
		return System.currentTimeMillis()+String.valueOf(new Random().nextInt(900)+100);
	}
	
	/**
	 * 获得 IP
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	
	/**
	 * 删除文件夹以及文件
	 *       PathUtil.getWebRootPath()
	 * @param filePath
	 * @return
	 */
	public static boolean deleteFile(String delpath){
		if(delpath == null){
			delpath = "";
		}
		File file = new File(delpath);  
		   // 当且仅当此抽象路径名表示的文件存在且 是一个目录时，返回 true  
		   if (!file.isDirectory()) {  
		    file.delete();  
		   } else if (file.isDirectory()) {  
		    String[] filelist = file.list();  
		    for (int i = 0; i < filelist.length; i++) {  
		     File delfile = new File(delpath + "\\" + filelist[i]);  
		     if (!delfile.isDirectory()) {  
		      delfile.delete();  
//		      System.out.println(delfile.getAbsolutePath() + "删除文件成功");  
		     } else if (delfile.isDirectory()) {  
		    	 deleteFile(delpath + "\\" + filelist[i]);  
		     }  
		    }  
//		    System.out.println(file.getAbsolutePath()+"删除成功");  
		    file.delete();  
		   }  
		  return true;  
	}
	
	/**
	 * 获得 页数
	 * @param page
	 * @return
	 */
	public static int getPage(Integer page){

		if(page == null || "".equals(page)){
			return 1;
		}else{
			return page;
		}
	}
	
	/**
	 * 获取指定格式的时间
	 * 
	 * yyyy-MM-dd HH:mm:ss
	 * 
	 * */
	public static String getFormatTime(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String result = sdf.format(date);
		return result;
	}
	
	
	
	
}
